function initPagination(elementId, totalRecords, currentPage, pageSize) {
    layui.use(['laypage'], function(){
        var laypage = layui.laypage;
        
        // 从URL中获取当前的page和limit参数
        var urlParams = new URLSearchParams(window.location.search);
        currentPage = parseInt(urlParams.get('page')) || currentPage;
        pageSize = parseInt(urlParams.get('limit')) || pageSize;

        // 计算实际的页数和每页显示数量
        pageSize = Math.min(pageSize, totalRecords);
        let totalPages = Math.max(1, Math.ceil(totalRecords / pageSize));

        // 确保当前页不超过总页数
        currentPage = Math.min(Math.max(1, currentPage), totalPages);

        laypage.render({
            elem: elementId,
            count: totalRecords,
            curr: currentPage,
            limit: pageSize,
            limits: [10, 20, 30, 50, 60, 70, 80, 90, 100],
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            jump: function(obj, first){
                if(!first){
                    var url = new URL(window.location.href);
                    url.searchParams.set('page', obj.curr);
                    url.searchParams.set('limit', obj.limit);
                    window.location.href = url.toString();
                }
            }
        });
    });
}